<template>
  <div class="xtx-infinite-loading" ref="container">
    <!-- 正在加载数据时显示 -->
    <div class="loading" v-if="!isLoad">
      <span class="img"></span>
      <span class="text">正在加载...</span>
    </div>
    <!-- 数据全部加载完毕时显示 -->
    <div class="none" v-if="!isFedir">
      <span class="text">亲，没有更多了</span>
    </div>
  </div>
</template>
<script>
import { useIntersectionObserver } from '@vueuse/core'
import { ref } from 'vue'
export default {
  name: 'InfiLoad',
  props: {
    isFedir: {
      type: Boolean,
      default: true
    },
    isLoad: {
      type: Boolean,
      default: true
    }
  },
  setup (props, { emit }) {
    const container = ref(null)
    useIntersectionObserver(
    // 监听目标元素
      container,
      ([{ isIntersecting }], observerElement) => {
      // ...判断  一旦为true 就发送ajax
        if (isIntersecting) {
          if (props.isFedir && props.isLoad) {
            emit('sub-page')
          }
        }
      },
      // 进入可视区域的比例是多少才执行回调 0-1 值越大 代表需要进入的面积越大
      { threshold: 0 }
    )
    return {
      container
    }
  }
}
</script>
<style scoped lang='less'>
.xtx-infinite-loading {
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .img {
      width: 50px;
      height: 50px;
      background: url(../../assets/images/load.gif) no-repeat center / contain;
    }
    .text {
      color: #999;
      font-size: 16px;
    }
  }
  .none {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .text {
      color: #999;
      font-size: 16px;
    }
  }
}
</style>
